package evs.gateway;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import evs.location.EvsException;

public class Util {

	private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
	public static synchronized Document getDocumentFromStream( InputStream stream ) throws EvsException
	{
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			return builder.parse( stream );
		} catch (ParserConfigurationException e) {
			throw new EvsException( e );
		} catch (SAXException e) {
			throw new EvsException( e );
		} catch (IOException e) {
			throw new EvsException( e );
		}
	}
	
	public static synchronized Document getDocumentFromString( String input ) throws EvsException
    {
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse( new InputSource( new StringReader( input ) ) );
        } catch (ParserConfigurationException e) {
            throw new EvsException( e );
        } catch (SAXException e) {
            throw new EvsException( e );
        } catch (IOException e) {
            throw new EvsException( e );
        }
    }
}
